package cn.web.tunnelrobot.service.impl;

import cn.web.tunnelrobot.dao.UserRecordDao;
import cn.web.tunnelrobot.dao.WarningRecordDao;
import cn.web.tunnelrobot.dto.WarningRecordDTO;
import cn.web.tunnelrobot.pojo.UserRecord;
import cn.web.tunnelrobot.service.WarningRecordService;
import cn.web.tunnelrobot.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 警告记录Service实现层
 * @Author:lichuang
 */
@Service
public class WarningRecordServiceImpl implements WarningRecordService {
    @Autowired
    private WarningRecordDao warningRecordDao;
    @Autowired
    private UserRecordDao userRecordDao;
    //获取用户session信息
    //HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    //User user = (User) request.getSession().getAttribute(Constants.USER_SESSION);
    @Override
    public HashMap<String, Object> getWarningRecordList(String wrLevel,Integer wrType, String wrDescribe, Integer wrAffirmStatus,Date wrWarningTime,Integer pageNo, Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        StringBuilder sbu = new StringBuilder();
        if(!StringUtils.isEmpty(wrLevel)){
            sbu.append("等级:"+wrLevel);
        }
        if(!StringUtils.isEmpty(wrDescribe)){
            sbu.append(",描述信息:"+wrDescribe);
        }
        if(wrAffirmStatus != null){
            if(wrAffirmStatus == 0){
                sbu.append(",确认状态:未确认");
            }else if(wrAffirmStatus == 1){
                sbu.append(",确认状态:已确认");
            }
        }
        if(wrWarningTime != null){
            sbu.append(",确认时间:"+wrWarningTime);
        }
        String content = "";//所有操作内容
        if(sbu.equals("")){
            content = sbu.append("查询警告列表信息").toString();
        }else{
            content = "查询警告列表信息,携带条件:"+sbu.toString();
        }
       /* UserRecord userRecord = new UserRecord();
        userRecord.setUsId(user.getUsId());
        userRecord.setUrOperatContent(content);
        userRecord.setUrOperatTime(new Date());
        userRecordDao.addUserRecord(userRecord);*/
        System.out.println("serviceimpl:"+wrLevel);
        try {
            System.out.println("serviceimpl try:"+wrLevel);
            List<WarningRecordDTO> warningRecordList  =  warningRecordDao.getWarningRecordList(wrLevel,wrType,wrDescribe,wrAffirmStatus, wrWarningTime,(pageNo-1)*pageSize,pageSize);
            if(warningRecordList.size() > 0){
                map.put("code",0);
                map.put("data",warningRecordList);
                int count =  warningRecordDao.getWarningRecordListCount(wrType,wrLevel,wrDescribe,wrAffirmStatus, wrWarningTime);
                if(count > 0){
                    map.put("count",count);
                }else{
                    map.put("count",0);
                }
                return map;
            }else{
                map.put("code",-1);
                map.put("data","暂无信息列表");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","查询信息列表出错");
            return map;
        }
    }

    @Override
    public HashMap<String,Object>  addWarningRecordInfo(String wrLevel,String wrDescribe) {
        HashMap<String, Object> map = new HashMap<>();
        try {
            WarningRecordDTO warningRecordDTO = new WarningRecordDTO();
            warningRecordDTO.setWrLevel(wrLevel);
            warningRecordDTO.setWrDescribe(wrDescribe);
            warningRecordDTO.setWrWarningTime(new Date());
            int result = warningRecordDao.addWarningRecordInfo(warningRecordDTO);
            if(result > 0){
                map.put("code",0);
                map.put("msg","添加成功");
                UserRecord userRecord = new UserRecord();
                userRecord.setUsId(1);
                userRecord.setUrOperatContent("(系统运行期间自动执行)添加警告信息,警告等级:"+wrLevel+",警告描述:"+wrDescribe+"");
                userRecord.setUrOperatTime(new Date());
                userRecordDao.addUserRecord(userRecord);
                return map;
            }else{
                map.put("code",-1);
                map.put("msg","添加失败");
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","添加预警信息时出错");
            return map;
        }
    }

    @Override
    public HashMap<String, Object> batchConfirm(Integer wrAffirmUser, Integer wrId, Integer wrAffirmStatus) {
        HashMap<String, Object> map = new HashMap<>();
        if(Constants.is_intranet == 1){
            map.put("code",-1);
            map.put("msg","外网没有权限");
            return map;
        }
        try {
            warningRecordDao.batchConfirm(wrAffirmUser,new Date(),wrAffirmStatus,wrId);
            map.put("code",0);
            map.put("msg","修改成功");
            /*UserRecord userRecord = new UserRecord();
            userRecord.setUsId(user.getUsId());
            userRecord.setUrOperatContent("批量审核警告信息");
            userRecord.setUrOperatTime(new Date());
            userRecordDao.addUserRecord(userRecord);*/
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code",-1);
            map.put("msg","修改失败");
            return map;
        }
    }
}
